package com.ggb.smartstudy.controller.admin;

import com.ggb.smartstudy.entity.Course;
import com.ggb.smartstudy.service.CourseService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gefangjie
 */
@RestController
@RequestMapping("/admin/course")
@Validated
public class CourseManageController {

    private final CourseService courseService;

    public CourseManageController(CourseService courseService) {
        this.courseService = courseService;
    }

    // 添加新课程
    @PostMapping("/add")
    public ResponseEntity<Course> addCourse(@Valid @RequestBody Course course) {
        courseService.insertCourse(course);
        return ResponseEntity.ok(course);
    }

    // 更新课程信息
    @PutMapping("/{id}")
    public ResponseEntity<Course> updateCourse(@PathVariable Integer id, @Valid @RequestBody Course course) {
        course.setId(id);
        courseService.updateCourse(course);
        return ResponseEntity.ok(course);
    }

    // 删除课程
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteCourse(@PathVariable Integer id) {
        courseService.deleteCourse(id);
        return ResponseEntity.noContent().build();
    }
}
